package cn.kk.api.feign.config;

import cn.kk.constant.HeaderConstant;
import cn.kk.utils.RequestUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class FeignClientInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        // 从请求头中获取用户id
        String uid = RequestUtil.getCurrentRequest().getHeader(HeaderConstant.UID_HEADER);
        if (StringUtils.isNotEmpty(uid)) {
            // 将用户ID添加到请求头中
            template.header(HeaderConstant.UID_HEADER, uid);
        }
    }

}